package logic

import (
	"context"
	"rental-system/models/cache"
	"rental-system/models/mysql"
	"time"

	"rental-system/rentalrpc/internal/svc"
	"rental-system/rentalrpc/rentalrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListRentalLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListRentalLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListRentalLogic {
	return &ListRentalLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListRentalLogic) ListRental(in *rentalrpc.ListRentalRequest) (*rentalrpc.ListRentalResponse, error) {
	// todo: add your logic here and delete this line

	var rental []mysql.Rental
	mysql.DB.Debug().Find(&rental)

	var rentalList []*rentalrpc.ListRental

	for _, m := range rental {
		list := rentalrpc.ListRental{
			Title:   m.Title,
			Content: m.Content,
			Price:   float32(m.Price),
			Address: m.Address,
		}
		rentalList = append(rentalList, &list)
	}

	cache.Rdb.Set(l.ctx, "rentalList", rentalList, time.Minute*30)

	return &rentalrpc.ListRentalResponse{List: rentalList}, nil
}
